let banner_id = "adunit-fb90769fd74ab162"
let video_id = "adunit-52bb18961f9d7735"
let video_start_id = "adunit-e48eb3ed3412601a"
let table_id = "adunit-d5c4e8da5470e48e"
let excitation_id = "adunit-f5daab2b9936b419"
let interstitialAd = null;

const table_ad = () => {
  return new Promise((reslove, reject) => {
    if (wx.createInterstitialAd) {
      interstitialAd = wx.createInterstitialAd({
        adUnitId: table_id
      })
      interstitialAd.onLoad(() => {
        table_ad_show()
      })
      interstitialAd.onError((err) => {
        console.log('table_ad-------------------' + err)
      })
      interstitialAd.onClose(() => {
        reslove()
      })
    }
  })
}

const table_ad_show = () => {
  if (interstitialAd) {
    interstitialAd.show().catch((err) => {
      console.error(err)
    })
  }
}

const excitation_ad = () => {
  return new Promise((reslove, reject) => {
    if (wx.createInterstitialAd) {
      interstitialAd = wx.createRewardedVideoAd({
        adUnitId: excitation_id
      })
      interstitialAd.onLoad(() => {
        reslove()
      })
      interstitialAd.onError((err) => {
        console.log('excitation_ad-------------------' + err)
      })
      interstitialAd.onClose((res) => {
        if (res && res.isEnded) {
          getApp().globalData.posts('/api/app/user/sorting/ouyoung_api/addhappiness_integral').then(r => {
            getApp().showModal(r.data.msg)
          })
        }
      })
    }
  })


}

const excitation_ad_show = () => {
  if (interstitialAd) {
    interstitialAd.show().catch((err) => {
      interstitialAd.load()
        .then(() => {
          interstitialAd.show()
        })
        .catch(err => {
          console.log('激励视频 广告显示失败')
        })
    })
  }
}

module.exports = {
  banner_id,
  video_id,
  video_start_id,
  table_id,
  excitation_id,
  table_ad,
  table_ad_show,
  excitation_ad,
  excitation_ad_show,
}